package e2e

import (
	"net/http/httptest"

	"github.com/gin-gonic/gin"
	"github.com/lab-online/pkg/auth"
	"github.com/lab-online/pkg/jwt"
	"gorm.io/gorm"
)

type Config struct {
	Database *gorm.DB
	Server   *gin.Engine
	JWT      *jwt.Config
	Prefix   string
}

type Context struct {
	JWT      jwt.JWTAction
	Database *gorm.DB
	Server   *gin.Engine
	Prefix   string
}

func NewContext(config *Config) *Context {
	jwtAction, _ := jwt.New(config.JWT)

	return &Context{
		Database: config.Database,
		Prefix:   config.Prefix,
		Server:   config.Server,
		JWT:      jwtAction,
	}
}

// JSONRequest 模拟 JSON 请求
func (ctx *Context) JSONRequest(request *Request) *httptest.ResponseRecorder {
	return JSONRequest(ctx.Server, request)
}

// GenBearerToken 生成 Bearer Token
func (ctx *Context) GenBearerToken(username string, role auth.Role) string {
	token, _ := ctx.JWT.GenToken(username, uint8(role))
	return "Bearer " + token
}
